package com.quanyan.stadium.biz.impl;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.quanyan.stadium.biz.BizGameOrderDao;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.*;
import com.quanyan.stadium.mapper.TbGameEventTeamMapper;
import com.quanyan.stadium.mapper.TbGameOrderMapper;
import com.quanyan.stadium.mapper.TbTeamDoubleAddendumMapper;
import com.quanyan.stadium.mapper.VStadiumUserDefinedMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zyq
 * Date: 2016/7/6 0006
 * Time: 16:33
 */

@Service
public class BizGameOrderDaoImpl implements BizGameOrderDao {

    @Autowired
    TbGameOrderMapper gameOrderMapper;

    @Autowired
    TbGameEventTeamMapper gameEventTeamMapper;

    @Autowired
    VStadiumUserDefinedMapper userDefinedMapper;

    @Autowired
    TbTeamDoubleAddendumMapper tbTeamDoubleAddendumMapper;

    @Override
    public TbGameOrder updateOrderByOrderTradeId(TbGameOrder gameOrder) {
        TbGameOrderExample tbGameOrderExample = new TbGameOrderExample();
        TbGameOrderExample.Criteria criteria = tbGameOrderExample.createCriteria();
        criteria.andTradeOrderNoEqualTo(gameOrder.getTradeOrderNo());
        int i = gameOrderMapper.updateByExampleSelective(gameOrder, tbGameOrderExample);
        if (1 == i) {
            List<TbGameOrder> orders = gameOrderMapper.selectByExample(tbGameOrderExample);
            if (!CollectionUtils.isEmpty(orders)) {
                return orders.get(0);
            }
        }
        return null;
    }

    @Override
    public TbGameEventTeam updateOrInsertTeam(TbGameEventTeam gameEventTeam) {
        TbGameEventTeamExample example = new TbGameEventTeamExample();
        if(Optional.fromNullable(gameEventTeam.getId()).isPresent()){
            TbGameEventTeamExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(gameEventTeam.getId());
            gameEventTeamMapper.updateByExampleSelective(gameEventTeam,example);
            return gameEventTeam;
        }else{
            gameEventTeamMapper.insert(gameEventTeam);
            return gameEventTeam;
        }
    }

    @Override
    public List<TbGameOrder> queryExpireOrder() {
        TbGameOrderExample example = new TbGameOrderExample();
        TbGameOrderExample.Criteria criteria = example.createCriteria();
        criteria.andDeadTimeLessThanOrEqualTo(new Date());
        criteria.andIsCloseEqualTo(GameConstants.ORDER_IS_CLOST_YES);
        criteria.andPayWaitNotEqualTo(GameConstants.EXPIRE_ORDER);
        return gameOrderMapper.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public Integer clearExpireOrder() {
        TbGameOrderExample example = new TbGameOrderExample();
        TbGameOrderExample.Criteria criteria = example.createCriteria();
        /**
         * 1.未完结
         * 2.支付状态：待支付
         * 3.订单状态：支付期间
         */
        criteria.andIsCloseEqualTo(GameConstants.ORDER_IS_CLOST_NO);
        criteria.andOrderStatusEqualTo(GameConstants.WAIT_ORDER);
        criteria.andPayWaitEqualTo(GameConstants.ON_PAYING);
        criteria.andDeadTimeLessThan(new Date());
        List<TbGameOrder> gameOrders = gameOrderMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(gameOrders)){
            //说明有过期订单
            List<Integer> list = Lists.newArrayList();
            for(TbGameOrder order :gameOrders){
                list.add(order.getGameEventId());
            }
            restoreShorage(list);
            TbGameOrder tbGameOrder = new TbGameOrder();
            tbGameOrder.setIsClose(GameConstants.ORDER_IS_CLOST_YES);
            tbGameOrder.setOrderStatus(GameConstants.EXPIRE_ORDER);
            tbGameOrder.setPayWait(GameConstants.FAIL_PAY);
            return gameOrderMapper.updateByExampleSelective(tbGameOrder,example);
        }
        return 0;
    }


    public Integer restoreShorage(List<Integer> ids){
        return userDefinedMapper.restoreStadiumAmount(ids,1);
    }

    @Override
    public void insertTbTeamDoubleAddendum(int gameEventId, List<Integer> gameTeamIds) {
        TbTeamDoubleAddendum addendum = new TbTeamDoubleAddendum();
        addendum.setGameEventId(gameEventId);
        Gson gson = new Gson();
        addendum.setTeamIdGson(gson.toJson(gameTeamIds));
        tbTeamDoubleAddendumMapper.insertSelective(addendum);
    }

    @Override
    public void delTbTeamDoubleAddendum(int gameEventId) {
        TbTeamDoubleAddendumExample example = new TbTeamDoubleAddendumExample();
        TbTeamDoubleAddendumExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        tbTeamDoubleAddendumMapper.deleteByExample(example);
    }

}
